package com.atguigu.blog.controller;

import com.atguigu.blog.pojo.Category;
import com.atguigu.blog.service.CategoryService;
import com.atguigu.blog.service.impl.CategoryServiceImpl;
import com.atguigu.blog.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author MikeCoder
 * @create 2023-11-2419:29
 * @description:
 * @verson:
 */
@RestController
@RequestMapping("/category")
@CrossOrigin
@Tag(name = "分类接口",description = "有关文章的接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping
    @Operation(summary = "查询所有分类")
    public Result<List<Category>> showAll(){
        List<Category> categoryList = categoryService.showAll();
        return Result.ok(categoryList);
    }

    @PostMapping("/{limit}/{page}")
    @Operation(summary = "根据分页条件查询分类")
    @Parameters({@Parameter(name = "limit",description = "每页数量",required = true),
                @Parameter(name = "page",description = "页码",required = true),
                @Parameter(name = "category",description = "分类对象",required = false)
    })
    public Result<Map<String,Object>> showByCname(@PathVariable Integer limit,
                                                  @PathVariable Integer page,
                                                  @RequestBody Category category
    ){
        Map<String, Object> map = categoryService.showByCname(limit, page, category);
        return Result.ok(map);
    }

    @PostMapping
    @Operation(summary = "添加分类")
    @Parameters(@Parameter(name = "category",description = "分类对象"))
    public Result add(@RequestBody Category category){
        Integer result = categoryService.add(category);
        if(result>0){
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    @PutMapping
    @Operation(summary = "修改分类")
    @Parameters(@Parameter(name = "category",description = "分类对象"))
    public Result change(@RequestBody Category category){
        Integer result = categoryService.change(category);
        if(result>0){
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    @GetMapping("/{cid}")
    @Operation(summary = "通过cid查找分类对象")
    @Parameters(@Parameter(name = "category",description = "分类对象"))
    public Result<Category> getCategoryByCid(@PathVariable Long cid){
        Category category = categoryService.getCategoryByCid(cid);
        return Result.ok(category);
    }

    @DeleteMapping("/{cid}")
    @Operation(summary = "删除分类")
    @Parameters(@Parameter(name = "cid",description = "分类id"))
    public Result change(@PathVariable Long cid){
        Integer result = categoryService.remove(cid);
        if(result>0){
            return Result.ok(null);
        }
        return Result.fail(null);
    }
}
